package com.example.thebasics;

import connection.ConnectionManager;
import data.Lista;
import android.os.AsyncTask;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;

public class RemoverListaActivity extends Activity {
	
	
	
	private ConnectionManager connectionManager;
	private Lista lista;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.remover_lista);
		lista = (Lista) getIntent().getSerializableExtra("Lista");
				
	
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.remover_lista, menu);
		return true;
	}
	
	public boolean removeLista(){
		connectionManager = new ConnectionManager();
		int row = connectionManager.removeLista(lista);
		System.out.println("Lista Removida - : Affected " + row + " row(s).");
		if (row != 0) {
			System.out.println("Lista Removida com Sucesso!");
			connectionManager.closeConnection();
			return true;
		}
		connectionManager.closeConnection();
		return false;
	}
	
	public void remover(View v){
		ConnectionRemoverListaTask crlt = new ConnectionRemoverListaTask(this);
		crlt.doInBackground("");
		finish();
	}
	
	public void cancelar(View v){
		finish();
	}

}

class ConnectionRemoverListaTask extends AsyncTask<String, Void, Boolean> {
	private RemoverListaActivity activity;

	public ConnectionRemoverListaTask(RemoverListaActivity activity) {
		this.activity = activity;
	}

	@Override
	protected Boolean doInBackground(String... params) {
		return activity.removeLista();
	}
}
